<?php
class Apk {
	public function getInfo($sFile) {
		if (is_file ( $sFile )) {
			$aInfo ['size'] = filesize ( $sFile );
			$oZip = zip_open ( $sFile );
			while ( ($sRow = zip_read ( $oZip )) !== false ) {
				if (zip_entry_name ( $sRow ) == 'AndroidManifest.xml') {
					$sTmpXml = tempnam ( dirname ( $sFile ), 'tmpxml_' );
					$rFp = fopen ( $sTmpXml, 'wb' );
					$iLength = zip_entry_filesize ( $sRow );
					$sString = zip_entry_read ( $sRow, $iLength );
					fwrite ( $rFp, $sString, $iLength );
					fclose ( $rFp );
					$sToTmpXml = $sTmpXml . '.xml';
					$sShell = '/usr/local/bin/java -jar ' . LIB . '/AXMLPrinter2.jar ' . $sTmpXml . '>' . $sToTmpXml;
					exec ( $sShell );
					$sXml = file_get_contents ( $sToTmpXml );
					if (preg_match ( '/android:versionCode="(.+?)"/', $sXml, $match )) {
						$aInfo ['version_code'] = $match [1];
					}
					if (preg_match ( '/android:versionName="(.+?)"/', $sXml, $match )) {
						$aInfo ['version_name'] = $match [1];
					}
					if (preg_match ( '/package="(.+?)"/', $sXml, $match )) {
						$aInfo ['package'] = $match [1];
					}
					unlink ( $sTmpXml );
					unlink ( $sToTmpXml );
				}
			}
			return $aInfo;
		} else {
			return false;
		}
	}
}